
// 引入路由组件
import Home from '@/pages/Home'
import Login from '@/pages/Login'
import Register from '@/pages/Register'
import Search from '@/pages/Search'
import Detail from '@/pages/Detail'
import AddCartSuccess from '@/pages/AddCartSuccess'
import ShopCart from '@/pages/ShopCart'
import Trade from '@/pages/Trade'
import Pay from '@/pages/Pay'
import PaySuccess from '@/pages/PaySuccess'
import Center from '@/pages/Center'
import MyOrder from '@/pages/Center/myOrder'
import GroupBuy from '@/pages/Center/groupOrder'

export default [
  {
    path:'/',
    redirect:'/home',
    // component: () => import('@/pages/Home'),
    // meta:{
    //   showFooter:true
    // }
  },
  {
    path:'/home',
    component:Home,
    meta:{
      showFooter:true
    },
    // beforeEnter: (to, from, next) => { // 路由前置守卫
    //   // 如果还没有登陆, 放行
    //   if (!store.state.user.userInfo.token) {
    //     next()
    //   } else {
    //     // 如果已经登陆, 跳转到首页
    //     next('/')
    //   }
    // } 
  },
  {
    path:'/login',
    component:Login,
    meta:{
      showFooter:false
    }
  },
  {
    path:'/register',
    component:Register,
    meta:{
      showFooter:false
    }
  },
  {
    path:'/search/:keyWord?',
    component:Search,
    name:'search',
    meta:{
      showFooter:true
    },
    // 可以将params参数作为路由身上的属性
    // props:true
    // 对象写法,额外给路由传递参数
    // props:{a:1,b:2}
    // 常用函数写法
    props:($route)=>{
      return {keyWord:$route.params.keyWord,k:$route.query.k}
    }
  },
  {
    //点击商品图片的时候，需要带上商品id
    path:'/detail/:skuId',
    component:Detail,
    meta:{
      showFooter:true
    },
  },
  {
    path: '/addCartSuccess',
    component: AddCartSuccess,
    meta:{
      showFooter:true
    },
    // 因为这里借口好像有问题
    beforeEnter (to, from, next) {
      // 得到当前路由信息对象
      // const route = router.currentRoute  // route就是from

      // 得到要跳转到目路由的query参数
      const skuNum = to.query.skuNum || 273
      // 读取保存的数据
      // const skuInfo = JSON.parse(window.sessionStorage.getItem('SKU_INFO_KEY'))
      const skuInfo = JSON.parse(window.sessionStorage.getItem('SKUINFO'))
      // console.log('---', skuNum, skuInfo)
      // 只有都存在, 才放行
      if (skuNum && skuInfo) {
        next()
      } else { // 在组件对象创建前强制跳转到首页
        next('/')
      }
    }
  },
  {
    path:'/shopCart',
    component:ShopCart,
    meta:{
      showFooter:true
    }
  },
  {
    path: '/trade',
    component: Trade,
    meta:{
      showFooter:true
    },
    /* 只能从购物车界面, 才能跳转到交易界面 */
    beforeEnter (to, from, next) {
      if (from.path==='/shopcart') {
        next()
      } else {
        // 其他路由，停留在当前
        next(false)
      }
    }
  },
  {
    path: '/pay',
    component: Pay,
    meta:{
      showFooter:true
    },
    // 将query参数映射成props传递给路由组件
    props: route => ({orderId: route.query.orderId}),

    /* 只能从交易界面, 才能跳转到支付界面 */
    beforeEnter (to, from, next) {
      if (from.path==='/trade') {
        next()
      } else {
        next(false)
      }
    }
  },
  {
    path: '/paysuccess',
    component: PaySuccess,
    meta:{
      showFooter:true
    },
    /* 只有从支付界面, 才能跳转到支付成功的界面 */
    beforeEnter (to, from, next) {
      if (from.path==='/pay') {
        next()
      } else {
        next('/pay')
      }
    }
  },
  {
    path: '/center',
    component: Center,
    meta:{
      showFooter:true
    },
    children: [
      {
        path:'/center',
        redirect:'/center/myorder'
      },
      {
        // path: '/center/myorder',
        path: 'myorder',
        component: MyOrder,
      },
      {
        path: 'groupbuy',
        component: GroupBuy,
      },
      // {
      //   path: '',
      //   redirect: 'myorder'
      // }
    ]
  },
  {
    path: '/communication',
    component: () => import('@/pages/Communication/Communication'),
    children: [
      {
        path: 'event',
        component: () => import('@/pages/Communication/EventTest/EventTest'),
        meta: {
          isHideFooter: true
        },
      },
      {
        path: 'model',
        component: () => import('@/pages/Communication/ModelTest/ModelTest'),
        meta: {
          isHideFooter: true
        },
      },
      {
        path: 'sync',
        component: () => import('@/pages/Communication/SyncTest/SyncTest'),
        meta: {
          isHideFooter: true
        },
      },
      {
        path: 'attrs-listeners',
        component: () => import('@/pages/Communication/AttrsListenersTest/AttrsListenersTest'),
        meta: {
          isHideFooter: true
        },
      },
      {
        path: 'children-parent',
        component: () => import('@/pages/Communication/ChildrenParentTest/ChildrenParentTest'),
        meta: {
          isHideFooter: true
        },
      },
      {
        path: 'scope-slot',
        component: () => import('@/pages/Communication/ScopeSlotTest/ScopeSlotTest'),
        meta: {
          isHideFooter: true
        },
      }
    ],
  },

]